# 导入控制台终端操作
from flask_script import Manager
# 导入迁移数据库模块
from flask_migrate import MigrateCommand, Migrate
# 导入app和db
from info import create_app, db
# 导入模型类
from info import models

# 调用工厂函数，传入开发模式的配置信息
from info.models import User

app = create_app('dev')
# 实例化Manager
manager = Manager(app)
# 使用迁移框架和程序实例以及sqlalchemy实例进行关联
Migrate(app, db)
# 添加迁移命令
manager.add_command('db', MigrateCommand)


@manager.option('-n', '-name', dest='name')
@manager.option('-p', '-password', dest='password')
def createsuperuser(name, password):
    """创建管理员用户"""
    if not all([name, password]):
        print('参数不足')
        return

    user = User()
    user.mobile = name
    user.nick_name = name
    user.password = password
    user.is_admin = True

    try:
        db.session.add(user)
        db.session.commit()
        print("创建成功")
    except Exception as e:
        app.logger.error(e)
        db.session.rollback()

if __name__ == '__main__':
    print(app.url_map)
    manager.run()
